package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyConfigInfoQuery;
import com.zl.tby.model.TbyConfigInfo;
import com.zl.tby.repository.TbyConfigInfoRepo;
import com.zl.tby.service.ITbyConfigInfoService;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 参数配置
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyConfigInfoServiceImpl implements ITbyConfigInfoService {

    @Db
    private TbyConfigInfoRepo tbyConfigInfoRepo;

    @Override
    @Tran
    public R<TbyConfigInfo> add(TbyConfigInfo entity) {
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyConfigInfoRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyConfigInfo> update(TbyConfigInfo entity) {
        if (entity.getId() == null) {
            return R.failed("数据不存在");
        }
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyConfigInfoRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        int flag = tbyConfigInfoRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyConfigInfo get(Long id) {
        return tbyConfigInfoRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyConfigInfo> page(TbyConfigInfoQuery query) {
        IPage<TbyConfigInfo> iPage = tbyConfigInfoRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getCode()), "code = ?", query.getCode());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyConfigInfo> list(TbyConfigInfoQuery query) {
        return tbyConfigInfoRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getCode()), "code = ?", query.getCode());
            if (query.getCodeList() != null && query.getCodeList().size() > 0) {
                mapperWhereQ.andIn("code", query.getCodeList());
            }
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
    }

    @Override
    public TbyConfigInfo getByCode(String code) {
        TbyConfigInfoQuery query = new TbyConfigInfoQuery();
        query.setCode(code);
        List<TbyConfigInfo> list = this.list(query);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public R<Boolean> checkCode(Long id, String code) {
        if (StringUtils.isEmpty(code)) {
            return R.ok(false);
        }
        List<TbyConfigInfo> dbList = this.tbyConfigInfoRepo.selectList(mp -> {
            mp.whereEq("code", code);
        });
        for (TbyConfigInfo data : dbList) {
            if (id == null && data.getCode().equals(code)) {
                return R.ok(true);
            } else if (id != null && data.getCode().equals(code) && id.compareTo(data.getId()) != 0) {
                return R.ok(true);
            }
        }
        return R.ok(false);
    }

    @Override
    public Map<String, String> getValueByCode(String codes) {
        Map<String, String> map = new HashMap<>(0);
        if (StringUtils.isEmpty(codes)) {
            return map;
        }
        TbyConfigInfoQuery query = new TbyConfigInfoQuery();
        query.setCodeList(Arrays.asList(ToolsUtil.splitStr(codes, ',')));
        List<TbyConfigInfo> dbList = this.list(query);
        for (TbyConfigInfo config : dbList) {
            map.put(config.getCode(), config.getContent());
        }
        return map;
    }
}
